package org.wkg.lesson04;

public class Demo_409 {

    public static void main(String[] args) throws InterruptedException {
        Counter409 counter = new Counter409();
        Counter409 counter1 = new Counter409();
        //在increase与increase1方法中，Counter409.class作为当前类，相当于静态变量，还是实例化对象的同一个锁对象，
        // 在实例化两个类的时候，调用这两个方法，由于是同一把锁，会造成锁竞争

        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                counter.increase();
            }
        });

        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 50000; i++) {
                counter1.increase();
            }
        });

        t1.start();
        t2.start();
        t1.join();
        t2.join();

        System.out.println("count : " + counter.count);
    }


}

class Counter409 {
    public static int count = 0;


    public void increase() {
        synchronized (Counter409.class) {
            count++;
        }
    }


}


